package com.uchedao.vehicledetection.activity;

import android.text.TextUtils;
import android.view.View;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.JsonObject;
import com.uchedao.vehicledetection.R;
import com.uchedao.vehicledetection.config.Api;
import com.uchedao.vehicledetection.network.HttpRequest;
import com.uchedao.vehicledetection.utils.JudgeUtil;
import com.uchedao.vehicledetection.utils.ProgressDialogUtil;
import com.uchedao.vehicledetection.utils.T;
import com.uchedao.vehicledetection.utils.ViewUtil;
import com.uchedao.vehicledetection.view.widget.MyEditView;

import java.util.HashMap;
import java.util.Map;

public class ResetPwdActivity extends CJDBaseActivity {

    private MyEditView mev_user_phone, mev_check_num, mev_user_key;

    @Override
    protected String getActivityTag() {
        return "ResetPwdActivity";
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_reset_pwd;
    }

    @Override
    protected void initView() {
        mev_user_phone = (MyEditView) findViewById(R.id.mev_user_phone);
        mev_check_num = (MyEditView) findViewById(R.id.mev_check_num);
        mev_user_key = (MyEditView) findViewById(R.id.mev_user_key);
    }

    @Override
    protected void initData() {
        setTitle("找回密码");
    }

    @Override
    protected void initListener() {
        mev_user_phone.setButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String _phone = mev_user_phone.getString();
                if (JudgeUtil.isMobileNO(_phone)) {
                    requestSMS(_phone);
                } else {
                    ViewUtil.showEditError(mev_user_phone.getEt_content(), getString(R.string.phone_style_is_error));
                }
            }
        });
        findViewById(R.id.btn_reset_pwd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                requestReset();
            }
        });
    }

    private void requestSMS(String phone) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("mobile", phone);
        Request request = HttpRequest.getRequest(HttpRequest.POST, Api.Action.VALID_CODE, map
                , new Response.Listener<JsonObject>() {
            @Override
            public void onResponse(JsonObject response) {
                ProgressDialogUtil.dismissProgressDlg();
                T.showShort(ResetPwdActivity.this, "验证码已下发，请注意查收");
                mev_user_phone.setCheck(true);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                ProgressDialogUtil.dismissProgressDlg();
                T.showShort(ResetPwdActivity.this, "验证码下发失败！");
            }
        });
        ProgressDialogUtil.showProgressDlg(this, "获取验证码");
        addQueue(request);
    }

    private void requestReset() {
        String _mobile = mev_user_phone.getString();
        if (TextUtils.isEmpty(_mobile) || !JudgeUtil.isMobileNO(_mobile)) {
            ViewUtil.showEditError(mev_user_phone.getEt_content(), getString(R.string.phone_style_is_error));
            return;
        }
        String _password = mev_user_key.getString();
        if (TextUtils.isEmpty(_password) || _password.length() < 6) {
            ViewUtil.showEditError(mev_user_key.getEt_content(), getString(R.string.hint_input_new_pwd));
            return;
        }
        String _valid_code = mev_check_num.getString();
        if (TextUtils.isEmpty(_valid_code)) {
            ViewUtil.showEditError(mev_check_num.getEt_content(), getString(R.string.hint_input_vcode));
            return;
        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("mobile", _mobile);
        map.put("password", _password);
        map.put("valid_code", _valid_code);
        Request request = HttpRequest.getRequest(HttpRequest.POST, Api.Action.RESET_PASSWORD, map
                , new Response.Listener<JsonObject>() {
            @Override
            public void onResponse(JsonObject response) {
                ProgressDialogUtil.dismissProgressDlg();
                T.showShort(ResetPwdActivity.this, R.string.reset_pwd_succeed);
                finish();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                ProgressDialogUtil.dismissProgressDlg();
                T.showShort(ResetPwdActivity.this, error.getMessage());
            }
        }, false);
        ProgressDialogUtil.showProgressDlg(this, "设置密码");
        addQueue(request);
    }
}
